<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

$uid = intval($_G['uid']);
if(empty($uid) || $uid == 0){
	showmessage('to_login', '', array(), array('showmsg' => true, 'login' => 1));
}

require_once libfile('function/core');
require_once libfile('function/database');
require_once libfile('function/vote');
require_once libfile('function/home');
require_once libfile('function/spacecp');

$action = getgpc('ac');
$aid = intval(getgpc('aid'));
if (empty($aid) || empty($uid) || (!empty($action) && !in_array($action, array('upload', 'select')))) {
	showmessage(lang('bid/template', 'setimg_no_refresh'), 'bid.php?mod=user', array(), array('alert' => 'error'));
	exit();
}

$page = empty($_GET['page'])? 1 : intval($_GET['page']);
if($page<1) $page=1;
$perpage = 12;
$perpage = mob_perpage($perpage);
$start = ($page-1)*$perpage;
ckstart($start, $perpage);

$user = get_user($uid);
$activity = get_activity($aid);
if ('upload' == $action) {
	$uploadfiles = pic_save($_FILES['attach'], 0, $activity['name']);
	if($uploadfiles && is_array($uploadfiles)) {
		$albumid = $uploadfiles['albumid'];
		$picid = $uploadfiles['picid'];
		$uploadStat = 1;
		if($albumid > 0) {
			album_update_pic($albumid);
		}

		$count = DB::update('vote_user', array('picid' => $uploadfiles['picid']), array('uid' => $uid, 'aid' => $aid));

		$message = lang('bid/template', 'setimg_success');
		showmessage($message, 'bid.php?mod=user&aid=$aid&uid=&$uid', array(), array('alert' => 'right'));
	} else {
		$uploadStat = $uploadfiles;
		showmessage($uploadStat, "bid.php?mod=setimg&aid=$aid&uid=&$uid", array(), array('alert' => 'error'));
	}
}
else if ('select' == $action) {

	$picid = getgpc('picid');
	$user = get_user($_G['uid']);
	$time = time();
	DB::update('vote_user', array('picid' => $picid, 'update_time' => $time), array('id' => $user['id']));
	$user['picid'] = $picid; $user['update_time'] = $time;
	$pic = DB::fetch_first(' select * from '.DB::table('home_pic').' where picid = '.$user['picid']);
	updatecache('activities');

	$message = lang('bid/template', 'setimg_success');
	showmessage($message, 'bid.php?mod=user', array(), array('alert' => 'right'));
}
else {
	$count = getcount('home_pic', array('albumid'=>0, 'uid'=>$uid));
	$list = array();
	$pricount = 0;
	if($count) {
		$query = DB::query("SELECT * FROM ".DB::table('home_pic')." WHERE uid = $uid ORDER BY dateline DESC LIMIT $start,$perpage");
		while ($value = DB::fetch($query)) {
			if($value['status'] == 0 || $value['uid'] == $_G['uid'] || $_G['adminid'] == 1) {
				$value['pic'] = pic_get($value['filepath'], 'album', $value['thumb'], $value['remote']);
				$list[] = $value;
			} else {
				$pricount++;
			}
		}
	}
	$multi = multi($count, $perpage, $page, "bid.php?mod=setimg");

	include_once template("bid/setimg");
}
?>
